package com.qualitair44.activity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.TreeMap;
import java.util.TreeSet;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.qualitair44.R;
import com.qualitair44.constantes.Constantes;
import com.qualitair44.constantes.ConstantesVilles;
import com.qualitair44.data.Mesure;
import com.qualitair44.data.utils.Retriever;

public class HistoryActivity extends Activity {
	
	TableLayout tableLayout;
	RelativeLayout relativeLayout;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.history_layout);
        
        tableLayout = (TableLayout) findViewById(R.id.tableLayout1);
        relativeLayout = (RelativeLayout) findViewById(R.id.main_layout);
               
        TextView t = (TextView)createTextView("En cours de chargement ...", this);
        t.setBackgroundResource(0);
        TableRow table = (TableRow)createTableView(this);
        tableLayout.addView(addTextViewToTableRow(table, t));
		
        relativeLayout.removeViewAt(0);
        relativeLayout.addView(tableLayout);
        setContentView(relativeLayout);
        
        new updateMesureCourante().execute(null);
        
    }
    
    
    public View createTextView(String s, Activity v) {
    	
    	TextView t = new TextView(v);
    	t.setText(s);
    	t.setBackgroundResource(R.drawable.cell_shape);
    	t.setPadding(5, 5, 5, 5);
    	t.setTextAppearance(v, android.R.style.TextAppearance_Large);
    	return t;
    	
    }
    
    public View createTableRow(String s, TextView textView) {
    	TableRow t = new TableRow(this);
    	t.setPadding(5, 5, 5, 5);
    	t.addView(textView);
    	
    	return t;
    }
    
    public View createTableView(Activity v) {
    	TableRow t = new TableRow(v);
    	return t;
    }
    
    public View addTextViewToTableRow(TableRow tableRow, TextView textView) {
    	
    	tableRow.addView(textView);
    	return tableRow;
    }
    
    public void createTableau(TreeMap<Integer,ArrayList<Mesure>> mList) {
    	
    	boolean first = false;
    	
//    	
//    	TableRow t = (TableRow)createTableView(this);
//    	
//    	//Set Date
//    	for(Mesure m : mList.get(111)) {
//    		TextView text;
//			if(!first) {
//				text =  (TextView)createTextView("", this);
//				first = !first;
//			}
//			else {
//				text = (TextView)createTextView(String.valueOf(m.getDate().getDay()), this);
//			}
//			
//			addTextViewToTableRow(t, text);
//    		
//    	}
//    	
//    	tableLayout.addView(t);
    	
    	for(Integer i : mList.keySet()) {
    		first = false;
			TableRow t = (TableRow)createTableView(this);
			for(Mesure m : mList.get(i)) {
				TextView text;
				if(!first) {
					text =  (TextView)createTextView(String.valueOf(m.getDate().getYear() + 1900), this);
					first = !first;
				}
				else {
					text = (TextView)createTextView(String.valueOf(m.getIndice()), this);
				}
				
				addTextViewToTableRow(t, text);
			}
			tableLayout.addView(t);
		}
    	
    	relativeLayout.removeViewAt(0);
    	tableLayout.removeViewAt(0);
        relativeLayout.addView(tableLayout);
        setContentView(relativeLayout);
    }
    
    
    private class updateMesureCourante extends AsyncTask<Void, Void, TreeMap<Integer,ArrayList<Mesure>>> {
    	
		@Override
		protected TreeMap<Integer,ArrayList<Mesure>> doInBackground(Void... params) {
			TreeMap<Integer,ArrayList<Mesure>> statistiques = new TreeMap<Integer, ArrayList<Mesure>>();
			Calendar c1 = Calendar.getInstance();
			c1.add(Calendar.DATE, 1);
			c1.add(Calendar.DATE, 1);
			statistiques.put(Calendar.getInstance().getTime().getYear(), Retriever.getATMOS(c1.getTime(), ConstantesVilles.NANTES)) ;
			
			for(int i= 1; i<Constantes.NB_ANNEE;i++)
			{
				Calendar c = Calendar.getInstance();
				c.add(Calendar.YEAR, -i);
				c.add(Calendar.DATE, 1);
				Date current = c.getTime();
				statistiques.put(c.getTime().getYear(), Retriever.getATMOS(current, ConstantesVilles.NANTES)) ;
			}
			return statistiques;
		}

		protected void onPostExecute(TreeMap<Integer,ArrayList<Mesure>> mList) {
			createTableau(mList);
		}

	}
    
}
